//
//  Controller10.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/3/6.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit
import AVKit
import AVFoundation

// videoBackground
class Controller10: UIViewController {

    let myPlayerController = AVPlayerViewController();
    let imageView = UIImageView();
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置AVPlayer
        setupAVPlayerContoller();
     
        let btn1 = UIButton(frame: CGRect(x: SCREEN_W * 0.5 - 100, y: SCREEN_H - 150, width: 200, height: 40));
        btn1.setTitle("登录", for: .normal);
        btn1.setTitleColor(.black, for: .normal);
        btn1.backgroundColor = .white;
        view.addSubview(btn1);
        
        let btn2 = UIButton(frame: CGRect(x: SCREEN_W * 0.5 - 100, y: SCREEN_H - 90, width: 200, height: 40));
        btn2.setTitle("注册", for: .normal);
        btn2.setTitleColor(.black, for: .normal);
        btn2.backgroundColor = .white;
        view.addSubview(btn2);
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        NotificationCenter.default.addObserver(self, selector: #selector(playerRepeat), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil);
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated);
        NotificationCenter.default.removeObserver(self);
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated);
        myPlayerController.player?.play();
        myPlayerController.videoGravity = AVLayerVideoGravityResizeAspectFill
        myPlayerController.player?.volume = 1.0;
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
 
    
    func setupAVPlayerContoller(){
        //设置view的层次。将player的view放在这个controller中
        view.addSubview(myPlayerController.view);
        myPlayerController.view.backgroundColor = .white
        //设置myPlayerController.view的大小
        var frame = view.bounds
        frame.size.height = 200
        frame.origin.y = 200
        myPlayerController.view.frame = frame;
        
        //设置myPlayerController 那个设置栏
        myPlayerController.showsPlaybackControls = false;
        
        //初始化AVPlayer
        let player = AVPlayer(url:URL(fileURLWithPath: Bundle.main.path(forResource: "moments.mp4", ofType: nil)!));
        myPlayerController.player = player;
    }
    
    
    
    func playerRepeat(){
        myPlayerController.player?.seek(to: kCMTimeZero);
        myPlayerController.player?.play();
    }
    
    
}
